﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using deviceManage.Models;
using deviceManage.vo;
using deviceManage.Filters;

namespace deviceManage.Controllers
{
    [CheckLoginAttribute]
    public class MessageController : Controller

    {
        private readonly DeviceCtx _context = null;


        public MessageController(DeviceCtx context)
        {
            _context = context;
        }
        public IActionResult Index()
        {

            return View();
        }

        /// <summary>
        /// 返回所有消息
        /// </summary>
        /// <returns></returns>
        public AjaxResult allMsg(int page, int limit, String name)
        {   
            AjaxResult r = new AjaxResult();
            r.Count = _context.Message.Count();
            r.Code = 0;
            r.Data = _context.Message 
                .OrderBy(p => p.CreateDate)
                .Skip((page - 1) * limit).Take(limit)
                .ToList();

            return r;
        }
        /// <summary>
        /// 获取所有提醒消息
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public AjaxResult allTipMsg(int page, int limit)
        {
            AjaxResult r = new AjaxResult();
            r.Count = _context.Message.Where(p=>p.Type==1).Count();
            r.Code = 0;
            r.Data = _context.Message.Where(p => p.Type == 1)
                .OrderBy(p => p.CreateDate)
                .Skip((page - 1) * limit).Take(limit)
                .ToList();

            return r;
        }
        /// <summary>
        /// 所有的预警信息
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public AjaxResult allWarnMsg(int page, int limit)
        {
            AjaxResult r = new AjaxResult();
            r.Count = _context.Message.Where(p => p.Type == 2).Count();
            r.Code = 0;
            r.Data = _context.Message.Where(p => p.Type == 2)
                .OrderBy(p => p.CreateDate)
                .Skip((page - 1) * limit).Take(limit)
                .ToList();

            return r;
        }
        /// <summary>
        /// 根据类型获取未读信息数量
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public AjaxResult getAllUnreadMessgeCount(int type)
        {
            AjaxResult r = new AjaxResult();

            var count = _context.Message.Where(p => p.Type == type && p.Readed==0).Count();
            r.Count = count;
            return r;
        }
        /// <summary>
        /// 删除消息
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public AjaxResult doDel(String[] ids)
        {
            AjaxResult r = new AjaxResult();
            for (int i=0;i<ids.Length;i++) {
                var Message = _context.Message.FirstOrDefault(m => m.Id == ids[i]);
                this._context.Remove(Message);
                this._context.SaveChanges();
            }
            return AjaxResult.GetOK("删除成功！");
        }
        /// <summary>
        /// 设置已读
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public AjaxResult doRead(String[] ids)
        {
            AjaxResult r = new AjaxResult();
            for (int i = 0; i < ids.Length; i++)
            {
                var Message = _context.Message.FirstOrDefault(m => m.Id == ids[i]);
                Message.Readed = 1;
                this._context.Update(Message);
                this._context.SaveChanges();
            }
            return AjaxResult.GetOK("设置成功！");
        }
        /// <summary>
        /// 设置未读
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public AjaxResult doUnRead(String[] ids)
        {
            AjaxResult r = new AjaxResult();
            for (int i = 0; i < ids.Length; i++)
            {
                var Message = _context.Message.FirstOrDefault(m => m.Id == ids[i]);
                Message.Readed = 0;
                this._context.Update(Message);
                this._context.SaveChanges();
            }
            return AjaxResult.GetOK("设置成功！");
        }


    }
}